package com.pj.project.sys_dict;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.protobuf.ServiceException;
import com.pj.project.sys_dict.entity.SysDictType;
import com.pj.project.sys_dict.mapper.SysDictDataMapper;
import com.pj.project.sys_dict.mapper.SysDictTypeMapper;
import com.pj.project.sys_dict.service.ISysDictTypeService;
import com.pj.project.sys_user.SysUser;
import com.pj.utils.sg.AjaxJson;
import com.pj.utils.so.SoMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController
{
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;


    @SaCheckPermission(SysDictType.PERMISSION_CODE)
    @PostMapping("/list")
    public AjaxJson list() {

        SoMap so = SoMap.getRequestSoMap();
        List<SysDictType> list = sysDictTypeMapper.getList(so.startPage());

        return AjaxJson.getPageData(so.getDataCount(), list);
    }


    /**
     * 查询字典类型详细
     */
    @PostMapping(value = "/{dictId}")
    public AjaxJson getInfo(@PathVariable Long dictId)
    {
        return AjaxJson.getSuccessData(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @PostMapping
    public AjaxJson add(@Validated  SysDictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return AjaxJson.getError("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
//        dict.setCreateBy("admin");
        return AjaxJson.getSuccessData(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @RequestMapping("/update")
    public AjaxJson edit(@Validated  SysDictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return AjaxJson.getError("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        return AjaxJson.getSuccessData(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictIds}")
    public AjaxJson remove(@PathVariable Long[] dictIds)  throws Exception
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return AjaxJson.getSuccess();
    }

    /**
     * 删除字典类型
     */
    @PostMapping("/del/{dictId}")
    public AjaxJson remove(@PathVariable Long dictId)  throws Exception
    {
//        dictTypeService.deleteDictTypeByIds(dictId);
        SysDictType dictType = dictTypeMapper.selectDictTypeById(dictId);
        if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0)
        {
            throw new ServiceException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
        }
        dictTypeMapper.deleteDictTypeById(dictId);

        return AjaxJson.getSuccess();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxJson optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return AjaxJson.getSuccessData(dictTypes);
    }
}
